package com.own.demo.geodemo;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.own.demo.utils.CommWriterFileInfo;
import com.own.demo.utils.FileToStringUtils;

import java.io.BufferedWriter;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class 长沙添加障碍物 {
    public static void main(String[] args) {
        System.out.println("开始执行");
        File geoZJ = new File("E:\\ownCode\\emg-geojson\\src\\test\\java\\com\\own\\demo\\geodemo\\geojsonLine.txt");
        String geoData = FileToStringUtils.txt2String(geoZJ).split("\\|")[0];
        JSONObject jsonObject= JSONObject.parseObject(geoData);
        JSONArray jsonArray=jsonObject.getJSONArray("features");
        System.out.println(jsonArray.size());
        List<String> data=new ArrayList();
        data.add("113.165766,28.340629;113.283182,28.301944;113.294855,28.216061;113.166453,28.200934;113.089548,28.287434;113.165766,28.340629");
        data.add("113.944422,28.375071;114.010340,28.363592;113.994547,28.297712;113.913523,28.341233;113.944422,28.375071");
        data.add("113.440560,28.146436;113.622632,28.102643;113.629529,28.024745;113.472285,28.045442;113.440560,28.146436");
        data.add("113.298489,28.471886;113.435043,28.485223;113.439181,28.434292;113.337110,28.424588;113.298489,28.471886");

        for(int i=0;i<data.size();i++){
            String string=data.get(i);
//            System.out.println(string);
            String[] gps= string.split("\\;");
            JSONArray gpsList=new JSONArray();
            for(int h=0;h<gps.length;h++) {
                String hh=gps[h];
//                System.out.println("---------"+hh);
                String[] points=hh.split("\\,");
                double[] gps2 = new double[2];

                gps2[0] =Double.valueOf(points[0]);
                gps2[1] =Double.valueOf(points[1]);
                gpsList.add(gps2);
            }
//            addLingString(jsonArray,i,1,"LineString",gpsList);

            addLingString(jsonArray,i,1,"Polygon",gpsList);
        }
//        writerFoFile("var polylineData1 = " + JSONObject.toJSONString(jsonObject));
    }

    public static void addLingString(JSONArray jsonArray, int i, int j, String geoType, JSONArray gpsList) {
        JSONObject geo = new JSONObject(4);
        geo.put("type", "Feature");
        JSONObject geoPro = new JSONObject(1);
        geoPro.put("name", i > 0 ? j + "-" + i : "area222");
        geo.put("properties", geoPro);
        JSONObject objectInfo = new JSONObject();
        objectInfo.put("coordinates", gpsList);
        objectInfo.put("type", geoType);
        geo.put("geometry", objectInfo);
        System.out.println(JSONObject.toJSONString(geo));
        jsonArray.add(geo);
    }

    public static void writerFoFile(String data) {
        try {
            String folderPath = "E:\\ownCode\\graphhopper-web-7.0\\地图工具";
            File folder = new File(folderPath);
            String fileName = "\\polylineData1.js";
            if (!folder.exists()) {
                boolean result = folder.mkdirs();
            }
            BufferedWriter bufferedWriter = CommWriterFileInfo.getWriterBuffer(folderPath + "\\" + fileName, false);
            CommWriterFileInfo.writer(bufferedWriter, data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
